{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T12:59:23.277845Z",
     "start_time": "2025-01-08T12:59:23.135441Z"
    }
   },
   "cell_type": "code",
   "source": "import numpy as np",
   "id": "25e3e4bfd1adf691",
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "##### np二维数组转置",
   "id": "cf5bbe87eb7017f4"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T12:59:23.282660Z",
     "start_time": "2025-01-08T12:59:23.278847Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 创建一个二维数组\n",
    "a = np.array([[1, 2, 3],\n",
    "              [4, 5, 6]])\n",
    "print(a)"
   ],
   "id": "decbd6e1f2690cc5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:59:23.288119Z",
     "start_time": "2025-01-08T12:59:23.283665Z"
    }
   },
   "source": [
    "# 使用 .T\n",
    "b = a.T\n",
    "print(\"转置后的数组:\\n\", b)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "转置后的数组:\n",
      " [[1 4]\n",
      " [2 5]\n",
      " [3 6]]\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T12:59:23.293048Z",
     "start_time": "2025-01-08T12:59:23.289535Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 使用 np.transpose()\n",
    "c = np.transpose(a)\n",
    "print(\"转置后的数组:\\n\", c)"
   ],
   "id": "10469021f66fffc4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "转置后的数组:\n",
      " [[1 4]\n",
      " [2 5]\n",
      " [3 6]]\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T12:59:23.298067Z",
     "start_time": "2025-01-08T12:59:23.294051Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#使用 np.swapaxes()\n",
    "d = np.swapaxes(a, 0, 1)\n",
    "print(\"转置后的数组:\\n\", d)"
   ],
   "id": "d2b91db61129b405",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "转置后的数组:\n",
      " [[1 4]\n",
      " [2 5]\n",
      " [3 6]]\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T12:59:23.303059Z",
     "start_time": "2025-01-08T12:59:23.299150Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 注意np.reshape()，只是重新调整数组的形状，而不会改变元素的顺序，不能实现转置\n",
    "e = a.reshape(a.shape[1], a.shape[0])\n",
    "print(e)\n",
    "print('该数组并非原数组的转置')"
   ],
   "id": "e54cc9a79f59bff7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "该数组并非原数组的转置\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "##### 交换轴",
   "id": "8a7ca78b26dea57c"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "用于交换数组的两个轴",
   "id": "5ab57b041249212"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T13:07:08.696609Z",
     "start_time": "2025-01-08T13:07:08.691570Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 创建三维数组\n",
    "a = np.array([[[1, 2],\n",
    "               [3, 4]],\n",
    "              [[5, 6],\n",
    "               [7, 8]]])\n",
    "\n",
    "# 交换第 0 轴和第 1 轴\n",
    "swapped_axis0_axis1 = np.swapaxes(a, axis1=0, axis2=1)\n",
    "print(\"交换第 0 轴和第 1 轴:\\n\", swapped_axis0_axis1)"
   ],
   "id": "fd17505d85466c9b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "交换第 0 轴和第 1 轴:\n",
      " [[[1 2]\n",
      "  [5 6]]\n",
      "\n",
      " [[3 4]\n",
      "  [7 8]]]\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T13:07:10.877332Z",
     "start_time": "2025-01-08T13:07:10.873969Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 交换第 1 轴和第 2 轴\n",
    "swapped_axis1_axis2 = np.swapaxes(a, axis1=1, axis2=2)\n",
    "print(\"交换第 1 轴和第 2 轴:\\n\", swapped_axis1_axis2)"
   ],
   "id": "db32f293086119fb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "交换第 1 轴和第 2 轴:\n",
      " [[[1 3]\n",
      "  [2 4]]\n",
      "\n",
      " [[5 7]\n",
      "  [6 8]]]\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "##### 轴滚动",
   "id": "574aafe4ad2f3b0e"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "array：输入的数组。\n",
    "\n",
    "axis：需要滚动的轴。\n",
    "\n",
    "start：目标位置。轴会被滚动到 start 之前的位置。"
   ],
   "id": "a51336e067cda5b0"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T12:59:41.356559Z",
     "start_time": "2025-01-08T12:59:41.352798Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 创建三维数组\n",
    "a = np.array([[[1, 2],\n",
    "               [3, 4]],\n",
    "              [[5, 6],\n",
    "               [7, 8]]])\n",
    "\n",
    "# 将第 2 轴滚动到第 0 轴之前\n",
    "rolled_axis2 = np.rollaxis(a, axis=2, start=0)\n",
    "print(\"将第 2 轴滚动到第 0 轴之前:\\n\", rolled_axis2)"
   ],
   "id": "6d02775d9d253427",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "将第 2 轴滚动到第 0 轴之前:\n",
      " [[[1 3]\n",
      "  [5 7]]\n",
      "\n",
      " [[2 4]\n",
      "  [6 8]]]\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T12:59:43.043623Z",
     "start_time": "2025-01-08T12:59:43.039882Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 将第 0 轴滚动到第 2 轴之前\n",
    "rolled_axis0 = np.rollaxis(a, axis=0, start=2)\n",
    "print(\"将第 0 轴滚动到第 2 轴之前:\\n\", rolled_axis0)"
   ],
   "id": "e29ff911ccf30f41",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "将第 0 轴滚动到第 2 轴之前:\n",
      " [[[1 2]\n",
      "  [5 6]]\n",
      "\n",
      " [[3 4]\n",
      "  [7 8]]]\n"
     ]
    }
   ],
   "execution_count": 10
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
